package com.example.springbott.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.example.springbott.bean.silic.AuthReCatchaBo;

import java.util.Random;

/**
 * 注册短信验证码
 * 需要注册阿里云并开通短信服务
 * @author silic xia
 * @since 2022/11/22 20:13
 */

public class WXAuthReCaptchaUtil {

    private static String smsCode;


    public static String getSMSReCatcha(AuthReCatchaBo authReCatchaBo) {
        smsCode = getRandomFiveDigits();
        String accessKeyId = "LTAI5t8gpxPTCR6W58RnZq4u";
        String accessKeySecret = "mVPbpbxia0JQotb7HyJAREV8QUuq8h";
        String signName = "stone4j";
        String templateCode = "SMS_173765187";
        String code = "{\"code\":\"" + smsCode + "\"}";


        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId,accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId","cn-hangzhou");
        String phoneNumber = authReCatchaBo.getMobile();
        request.putQueryParameter("PhoneNumbers",phoneNumber);
        request.putQueryParameter("SignName",signName);
        request.putQueryParameter("TemplateCode",templateCode);
        request.putQueryParameter("TemplateParam",code);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return smsCode;
    }

    private static String getRandomFiveDigits() {
        Random random = new Random();
        String number = "";
        for (int i = 0; i < 5; i++) {
            number += random.nextInt(10);
        }
        return number;
    }
}

